<?php

class ToolbaseDatabase {
       
        protected $instance;
        protected $manager;
       
        protected $server, $dbname, $server_id;
       
        protected function __construct( ToolbaseDatabaseManager $manager, $server, $dbname ) {
               
                ProjectConfiguration::registerZend();
               
                if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled')) {
                        $timer = sfTimerManager::getTimer('Database (Zend)');
                }
               
                $this->manager = $manager;
                $this->server = $server;
                $this->dbname = $dbname;
               
                $ini = $manager->getConfig();
               
                $this->instance = Zend_Db::factory('Pdo_Mysql', array(
                        'host'     => $server,
                        'username' => $ini['user'],
                        'password' => $ini['password'],
                        'dbname'   => $dbname,
                        'profiler' => sfConfig::get('sf_debug', false)
                ));
               
                if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled')) {
                        $timer->addTime();
                }
        }
       
        public function getObject() {
                if( !is_object( $this->instance ) ) {
                        throw new ToolDBException( 'No DB object has been initialized' );
                }
               
                return $this->instance;
        }
       
        public function getReplagServer() {
       
                $server = sfConfig::get( 'app_replag_servers_s' . $this->server_id, sfConfig::get( 'app_replag_servers_s1' ) );
               
                return $this->manager->getServerFromName( $server['serv'], $server['db'] );
        }
       
        public static function newFromLang( ToolbaseDatabaseManager $manager = null, $lang = 'en', $wiki = 'wikipedia' ) {
               
                $ts_db = $manager->getServerFromName( 'sql', 'toolserver' );
               
                $select = $ts_db
                        ->getObject()
                        ->select()
                        ->from( array( 'w' => 'wiki' ), array( 'w.dbname', 'w.server' ) )
                        ->where( 'domain = ?', "$lang.$wiki.org" )
                        ->query()
                        ->fetchAll();
               
                if( !count( $select ) ) {
                        throw new ToolDBNoWikiException( $lang, $wiki );
                }
               
                $server = self::newFromServer( $manager, 'sql-s' . $select[0]['server'] . '-rr', $select[0]['dbname'] );
                $server->setServerId( $select[0]['server'] );
                return $server;
        }
       
        public static function newFromServer( ToolbaseDatabaseManager $manager = null, $server = 'sql-s1', $dbname = 'enwiki_p' ) {
               
                return new self( $manager, $server, $dbname );
               
        }
       
        public function getDbname() {
                return $this->dbname;//For tests
        }
       
        public function setServerId( $server_id ) {
                $this->server_id = $server_id;
        }
       
}

